home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / APWB6U (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.5 KB  |  484 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.event.DocumentEvent;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Rectangle;
  7. import java.awt.Shape;
  8.  
  9. public class BoxView extends CompositeView {
  10.    int axis;
  11.    int width;
  12.    int height;
  13.    boolean xValid;
  14.    boolean yValid;
  15.    int[] preferredSpan = new int[2];
  16.    int[] resizeWeight = new int[2];
  17.    float[] alignment = new float[2];
  18.    boolean xAllocValid;
  19.    int[] xOffsets;
  20.    int[] xSpans;
  21.    boolean yAllocValid;
  22.    int[] yOffsets;
  23.    int[] ySpans;
  24.  
  25.    public BoxView(Element elem, int axis) {
  26.       super(elem);
  27.       this.axis = axis;
  28.    }
  29.  
  30.    void calculateAlignedPositions(int allocated, int axis) {
  31.       int[] offsets = axis == 0 ? this.xOffsets : this.yOffsets;
  32.       int[] spans = axis == 0 ? this.xSpans : this.ySpans;
  33.       int totalBelow = (int)((float)allocated * this.alignment[axis]);
  34.       int totalAbove = allocated - totalBelow;
  35.       int n = ((CompositeView)this).getViewCount();
  36.  
  37.       for(int i = 0; i < n; ++i) {
  38.          View v = ((CompositeView)this).getView(i);
  39.          float align = v.getAlignment(axis);
  40.          int span = (int)v.getPreferredSpan(axis);
  41.          int below = (int)((float)span * align);
  42.          int above = span - below;
  43.          if (v.getResizeWeight(axis) > 0) {
  44.             below = totalBelow;
  45.             above = totalAbove;
  46.          }
  47.  
  48.          offsets[i] = totalBelow - below;
  49.          spans[i] = below + above;
  50.       }
  51.  
  52.    }
  53.  
  54.    void calculateAlignedSizeRequirements(int axis) {
  55.       int totalAbove = 0;
  56.       int totalBelow = 0;
  57.       int n = ((CompositeView)this).getViewCount();
  58.  
  59.       for(int i = 0; i < n; ++i) {
  60.          View v = ((CompositeView)this).getView(i);
  61.          int span = (int)v.getPreferredSpan(axis);
  62.          int below = (int)(v.getAlignment(axis) * (float)span);
  63.          int above = span - below;
  64.          totalAbove = Math.max(above, totalAbove);
  65.          totalBelow = Math.max(below, totalBelow);
  66.          int[] var10000 = this.resizeWeight;
  67.          var10000[axis] += v.getResizeWeight(axis);
  68.       }
  69.  
  70.       this.preferredSpan[axis] = totalAbove + totalBelow;
  71.       this.alignment[axis] = 0.5F;
  72.       if (this.preferredSpan[axis] > 0) {
  73.          this.alignment[axis] = (float)totalBelow / (float)this.preferredSpan[axis];
  74.       }
  75.  
  76.    }
  77.  
  78.    void calculateTiledPositions(int allocated, int axis) {
  79.       int[] offsets = axis == 0 ? this.xOffsets : this.yOffsets;
  80.       int[] spans = axis == 0 ? this.xSpans : this.ySpans;
  81.       int totalPlay = allocated - this.preferredSpan[axis];
  82.       int totalWeight = this.resizeWeight[axis];
  83.       int totalOffset = 0;
  84.       int n = ((CompositeView)this).getViewCount();
  85.  
  86.       for(int i = 0; i < n; ++i) {
  87.          View v = ((CompositeView)this).getView(i);
  88.          offsets[i] = totalOffset;
  89.          int span = (int)v.getPreferredSpan(axis);
  90.          int weight = v.getResizeWeight(axis);
  91.          if (weight != 0 && totalWeight != 0) {
  92.             float factor = (float)(weight / totalWeight);
  93.             span = (int)((float)span + (float)totalPlay * factor);
  94.          }
  95.  
  96.          spans[i] = span;
  97.          totalOffset += span;
  98.       }
  99.  
  100.    }
  101.  
  102.    void calculateTiledSizeRequirements(int axis) {
  103.       this.alignment[axis] = 0.5F;
  104.       this.preferredSpan[axis] = 0;
  105.       this.resizeWeight[axis] = 0;
  106.       int n = ((CompositeView)this).getViewCount();
  107.  
  108.       for(int i = 0; i < n; ++i) {
  109.          View v = ((CompositeView)this).getView(i);
  110.          int[] var10000 = this.preferredSpan;
  111.          var10000[axis] = (int)((float)var10000[axis] + v.getPreferredSpan(axis));
  112.          var10000 = this.resizeWeight;
  113.          var10000[axis] += v.getResizeWeight(axis);
  114.       }
  115.  
  116.    }
  117.  
  118.    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  119.       Element elem = ((View)this).getElement();
  120.       Rectangle alloc = a != null && this.isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
  121.       int x = 0;
  122.       int y = 0;
  123.       int width = 0;
  124.       int height = 0;
  125.       if (alloc != null) {
  126.          x = alloc.x;
  127.          y = alloc.y;
  128.          width = alloc.width;
  129.          height = alloc.height;
  130.       }
  131.  
  132.       int index0 = elem.getElementIndex(e.getOffset());
  133.       int index1 = elem.getElementIndex(e.getOffset() + Math.max(e.getLength() - 1, 0));
  134.  
  135.       for(int i = index0; i <= index1; ++i) {
  136.          View v = ((CompositeView)this).getView(i);
  137.          if (alloc != null) {
  138.             alloc.x = x + this.xOffsets[i];
  139.             alloc.y = y + this.yOffsets[i];
  140.             alloc.width = this.xSpans[i];
  141.             alloc.height = this.ySpans[i];
  142.          }
  143.  
  144.          v.changedUpdate(e, alloc, f);
  145.       }
  146.  
  147.       DocumentEvent.ElementChange ec = e.getChange(elem);
  148.       if (ec != null) {
  149.          Element[] removedElems = ec.getChildrenRemoved();
  150.          Element[] addedElems = ec.getChildrenAdded();
  151.          View[] added = new View[addedElems.length];
  152.  
  153.          for(int i = 0; i < addedElems.length; ++i) {
  154.             added[i] = f.create(addedElems[i]);
  155.          }
  156.  
  157.          this.replace(ec.getIndex(), removedElems.length, added);
  158.       }
  159.  
  160.       if (a != null && !this.isAllocationValid()) {
  161.          Component c = ((View)this).getContainer();
  162.          c.repaint(x, y, width, height);
  163.       }
  164.  
  165.    }
  166.  
  167.    void checkRequests() {
  168.       if (this.axis == 0) {
  169.          if (!this.xValid) {
  170.             this.calculateTiledSizeRequirements(0);
  171.          }
  172.  
  173.          if (!this.yValid) {
  174.             this.calculateAlignedSizeRequirements(1);
  175.          }
  176.       } else {
  177.          if (!this.xValid) {
  178.             this.calculateAlignedSizeRequirements(0);
  179.          }
  180.  
  181.          if (!this.yValid) {
  182.             this.calculateTiledSizeRequirements(1);
  183.          }
  184.       }
  185.  
  186.       this.yValid = true;
  187.       this.xValid = true;
  188.    }
  189.  
  190.    protected void childAllocation(int index, Rectangle alloc) {
  191.       alloc.x += this.xOffsets[index];
  192.       alloc.y += this.yOffsets[index];
  193.       alloc.width = this.xSpans[index];
  194.       alloc.height = this.ySpans[index];
  195.    }
  196.  
  197.    public float getAlignment(int axis) {
  198.       this.checkRequests();
  199.       switch (axis) {
  200.          case 0:
  201.          case 1:
  202.             return this.alignment[axis];
  203.          default:
  204.             throw new IllegalArgumentException("Invalid axis: " + axis);
  205.       }
  206.    }
  207.  
  208.    public final int getHeight() {
  209.       return this.height;
  210.    }
  211.  
  212.    public float getPreferredSpan(int axis) {
  213.       this.checkRequests();
  214.       switch (axis) {
  215.          case 0:
  216.             return (float)(this.preferredSpan[axis] + ((CompositeView)this).getLeftInset() + ((CompositeView)this).getRightInset());
  217.          case 1:
  218.             return (float)(this.preferredSpan[axis] + ((CompositeView)this).getTopInset() + ((CompositeView)this).getBottomInset());
  219.          default:
  220.             throw new IllegalArgumentException("Invalid axis: " + axis);
  221.       }
  222.    }
  223.  
  224.    public int getResizeWeight(int axis) {
  225.       this.checkRequests();
  226.       switch (axis) {
  227.          case 0:
  228.          case 1:
  229.             return this.resizeWeight[axis];
  230.          default:
  231.             throw new IllegalArgumentException("Invalid axis: " + axis);
  232.       }
  233.    }
  234.  
  235.    protected View getViewAtPoint(int x, int y, Rectangle alloc) {
  236.       int n = ((CompositeView)this).getViewCount();
  237.       if (this.axis == 0) {
  238.          if (x < alloc.x + this.xOffsets[0]) {
  239.             this.childAllocation(0, alloc);
  240.             return ((CompositeView)this).getView(0);
  241.          } else {
  242.             for(int i = 0; i < n; ++i) {
  243.                if (x < alloc.x + this.xOffsets[i]) {
  244.                   this.childAllocation(i - 1, alloc);
  245.                   return ((CompositeView)this).getView(i - 1);
  246.                }
  247.             }
  248.  
  249.             this.childAllocation(n - 1, alloc);
  250.             return ((CompositeView)this).getView(n - 1);
  251.          }
  252.       } else if (y < alloc.y + this.yOffsets[0]) {
  253.          this.childAllocation(0, alloc);
  254.          return ((CompositeView)this).getView(0);
  255.       } else {
  256.          for(int i = 0; i < n; ++i) {
  257.             if (y < alloc.y + this.yOffsets[i]) {
  258.                this.childAllocation(i - 1, alloc);
  259.                return ((CompositeView)this).getView(i - 1);
  260.             }
  261.          }
  262.  
  263.          this.childAllocation(n - 1, alloc);
  264.          return ((CompositeView)this).getView(n - 1);
  265.       }
  266.    }
  267.  
  268.    public final int getWidth() {
  269.       return this.width;
  270.    }
  271.  
  272.    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  273.       Element elem = ((View)this).getElement();
  274.       DocumentEvent.ElementChange ec = e.getChange(elem);
  275.       if (ec != null) {
  276.          Element[] removedElems = ec.getChildrenRemoved();
  277.          Element[] addedElems = ec.getChildrenAdded();
  278.          View[] added = new View[addedElems.length];
  279.  
  280.          for(int i = 0; i < addedElems.length; ++i) {
  281.             added[i] = f.create(addedElems[i]);
  282.          }
  283.  
  284.          this.replace(ec.getIndex(), removedElems.length, added);
  285.          if (a != null) {
  286.             this.preferenceChanged((View)null, true, true);
  287.             ((View)this).getContainer().repaint();
  288.          }
  289.       }
  290.  
  291.       Rectangle alloc = a != null && this.isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
  292.       int pos = e.getOffset();
  293.       View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
  294.       if (v != null) {
  295.          v.insertUpdate(e, alloc, f);
  296.          if (v.getStartOffset() == pos && pos > 0) {
  297.             v = ((CompositeView)this).getViewAtPosition(pos - 1, alloc);
  298.             v.insertUpdate(e, alloc, f);
  299.          }
  300.       }
  301.  
  302.    }
  303.  
  304.    protected boolean isAfter(int x, int y, Rectangle innerAlloc) {
  305.       if (this.axis == 0) {
  306.          return x > innerAlloc.width + innerAlloc.x;
  307.       } else {
  308.          return y > innerAlloc.height + innerAlloc.y;
  309.       }
  310.    }
  311.  
  312.    protected boolean isAllocationValid() {
  313.       return this.xAllocValid && this.yAllocValid;
  314.    }
  315.  
  316.    protected boolean isBefore(int x, int y, Rectangle innerAlloc) {
  317.       if (this.axis == 0) {
  318.          return x < innerAlloc.x;
  319.       } else {
  320.          return y < innerAlloc.y;
  321.       }
  322.    }
  323.  
  324.    protected void layout(int width, int height) {
  325.       this.checkRequests();
  326.       if (this.xSpans == null) {
  327.          int n = ((CompositeView)this).getViewCount();
  328.          this.xSpans = new int[n];
  329.          this.ySpans = new int[n];
  330.          this.xOffsets = new int[n];
  331.          this.yOffsets = new int[n];
  332.       }
  333.  
  334.       if (this.axis == 0) {
  335.          if (!this.xAllocValid) {
  336.             this.calculateTiledPositions(width, 0);
  337.          }
  338.  
  339.          if (!this.yAllocValid) {
  340.             this.calculateAlignedPositions(height, 1);
  341.          }
  342.       } else {
  343.          if (!this.xAllocValid) {
  344.             this.calculateAlignedPositions(width, 0);
  345.          }
  346.  
  347.          if (!this.yAllocValid) {
  348.             this.calculateTiledPositions(height, 1);
  349.          }
  350.       }
  351.  
  352.       this.xAllocValid = true;
  353.       this.yAllocValid = true;
  354.       int n = ((CompositeView)this).getViewCount();
  355.  
  356.       for(int i = 0; i < n; ++i) {
  357.          View v = ((CompositeView)this).getView(i);
  358.          v.setSize((float)this.xSpans[i], (float)this.ySpans[i]);
  359.       }
  360.  
  361.    }
  362.  
  363.    public Shape modelToView(int pos, Shape a) throws BadLocationException {
  364.       if (!this.isAllocationValid()) {
  365.          Rectangle alloc = a.getBounds();
  366.          this.setSize((float)alloc.width, (float)alloc.height);
  367.       }
  368.  
  369.       return super.modelToView(pos, a);
  370.    }
  371.  
  372.    public void paint(Graphics g, Shape allocation) {
  373.       Rectangle alloc = allocation.getBounds();
  374.       this.setSize((float)alloc.width, (float)alloc.height);
  375.       int n = ((CompositeView)this).getViewCount();
  376.       int x = alloc.x + ((CompositeView)this).getLeftInset();
  377.       int y = alloc.y + ((CompositeView)this).getTopInset();
  378.       Rectangle clip = g.getClipBounds();
  379.  
  380.       for(int i = 0; i < n; ++i) {
  381.          alloc.x = x + this.xOffsets[i];
  382.          alloc.y = y + this.yOffsets[i];
  383.          alloc.width = this.xSpans[i];
  384.          alloc.height = this.ySpans[i];
  385.          if (alloc.intersects(clip)) {
  386.             this.paintChild(g, alloc, i);
  387.          }
  388.       }
  389.  
  390.    }
  391.  
  392.    protected void paintChild(Graphics g, Rectangle alloc, int index) {
  393.       View child = ((CompositeView)this).getView(index);
  394.       child.paint(g, alloc);
  395.    }
  396.  
  397.    public void preferenceChanged(View child, boolean width, boolean height) {
  398.       if (width) {
  399.          this.xValid = false;
  400.          this.xAllocValid = false;
  401.       }
  402.  
  403.       if (height) {
  404.          this.yValid = false;
  405.          this.yAllocValid = false;
  406.       }
  407.  
  408.       super.preferenceChanged(child, width, height);
  409.    }
  410.  
  411.    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  412.       Element elem = ((View)this).getElement();
  413.       DocumentEvent.ElementChange ec = e.getChange(elem);
  414.       boolean shouldForward = true;
  415.       if (ec != null) {
  416.          Element[] removedElems = ec.getChildrenRemoved();
  417.          Element[] addedElems = ec.getChildrenAdded();
  418.          View[] added = new View[addedElems.length];
  419.  
  420.          for(int i = 0; i < addedElems.length; ++i) {
  421.             added[i] = f.create(addedElems[i]);
  422.          }
  423.  
  424.          this.replace(ec.getIndex(), removedElems.length, added);
  425.          if (added.length != 0) {
  426.             shouldForward = false;
  427.          }
  428.  
  429.          if (a != null) {
  430.             this.preferenceChanged((View)null, true, true);
  431.             ((View)this).getContainer().repaint();
  432.          }
  433.       }
  434.  
  435.       if (shouldForward) {
  436.          Rectangle alloc = a != null && this.isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
  437.          int pos = e.getOffset();
  438.          View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
  439.          if (v != null) {
  440.             v.removeUpdate(e, alloc, f);
  441.          }
  442.       }
  443.  
  444.    }
  445.  
  446.    public void replace(int offset, int length, View[] elems) {
  447.       super.replace(offset, length, elems);
  448.       this.xOffsets = null;
  449.       this.xSpans = null;
  450.       this.xValid = false;
  451.       this.xAllocValid = false;
  452.       this.yOffsets = null;
  453.       this.ySpans = null;
  454.       this.yValid = false;
  455.       this.yAllocValid = false;
  456.    }
  457.  
  458.    public void setSize(float width, float height) {
  459.       if ((int)width != this.width) {
  460.          this.xAllocValid = false;
  461.       }
  462.  
  463.       if ((int)height != this.height) {
  464.          this.yAllocValid = false;
  465.       }
  466.  
  467.       if (!this.xAllocValid || !this.yAllocValid) {
  468.          this.width = (int)width;
  469.          this.height = (int)height;
  470.          this.layout(this.width - ((CompositeView)this).getLeftInset() - ((CompositeView)this).getRightInset(), this.height - ((CompositeView)this).getTopInset() - ((CompositeView)this).getBottomInset());
  471.       }
  472.  
  473.    }
  474.  
  475.    public int viewToModel(float x, float y, Shape a) {
  476.       if (!this.isAllocationValid()) {
  477.          Rectangle alloc = a.getBounds();
  478.          this.setSize((float)alloc.width, (float)alloc.height);
  479.       }
  480.  
  481.       return super.viewToModel(x, y, a);
  482.    }
  483. }
  484.